package net.fornwall.eclipsecoder.javasupport;

import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.AssertStatement;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ReturnStatement;

/**
 * At the moment the only use of this class is to remove assertions and all
 * methods whose interval contains a element in the removeList.
 * 
 * For unused code removal the SearchEngine probably gives a better and higher
 * level interfaces.
 */
public class Visitor extends ASTVisitor {

	private List<Integer> removeList;

	/**
	 * 
	 * @param removeList
	 *            Input parameter, read from this list.
	 * @param removeIntervals
	 *            To fill in with intervals to remove.
	 */
	public Visitor(List<Integer> removeList) {
		this.removeList = removeList;
	}

	/** Delete all assertions */
	@Override
	public boolean visit(AssertStatement node) {
		node.delete();
		return false;
	}

	@Override
	public boolean visit(CompilationUnit node) {
		return true;
	}

	@Override
	public boolean visit(LineComment node) {
		return super.visit(node);
	}

	@Override
	public boolean visit(MethodDeclaration node) {
		return true;
	}

	@Override
	public boolean visit(MethodInvocation node) {
		for (int i : removeList) {
			if (i > node.getStartPosition()
					&& i < node.getStartPosition() + node.getLength()) {
				node.getParent().delete();
			}
		}
		return true;
	}

	@Override
	public boolean visit(ReturnStatement node) {
		return true;
	}
}